rm(list=ls())
library(tidyverse)
library(plyr)
library(scales)
library(dplyr)
library(rmeta)
library(xtable)
library(stargazer)




states = tolower(c("AL", "AR", "CA", "CT", "FL", "IL", "IN", "KS", "KY", "MA", "MD",
                   "MO",
                   "NE", "NV", "NY", "OH", "OR", "PA", "RI", "SC", "TN", "TX", "VA", "WA",
                   "WV"))



results = as.data.frame(matrix(nrow = 0, ncol = 7))
colnames(results) = c("coef", "se", "race", "dv", "p value", "endog", "state")



for(state in states){
  f = paste0('results/results_',state,'_other_elections.Rdata')
  
  load(f)
  holder = as.data.frame(matrix(nrow = 60, ncol = 7))
  colnames(holder) = c("coef", "se", "race", "dv", "p value", "endog", "state")
  
  holder$coef = c(ivmod1_white$cace,  ivmod1_black$cace, ivmod1_hispanic$cace,
                  ivmod2_white$cace,  ivmod2_black$cace, ivmod2_hispanic$cace,
                  ivmod3_white$cace,  ivmod3_black$cace, ivmod3_hispanic$cace,
                  ivmod4_white$cace,  ivmod4_black$cace, ivmod4_hispanic$cace,
                  ivmod5_white$cace,  ivmod5_black$cace, ivmod5_hispanic$cace,
                  ivmod6_white$cace,  ivmod6_black$cace, ivmod6_hispanic$cace,
                  ivmod_rep_white$cace,  ivmod_rep_black$cace, ivmod_rep_hispanic$cace,
                  ivmod7_white$cace,  ivmod7_black$cace, ivmod7_hispanic$cace,
                  ivmod8_white$cace, ivmod8_black$cace, ivmod8_hispanic$cace,
                  ivmod9_white$cace,  ivmod9_black$cace, ivmod9_hispanic$cace,
                  ivmod10_white$cace,  ivmod10_black$cace, ivmod10_hispanic$cace,
                  
                  
                  
                  
                  ivmod_04_1_white$cace,  ivmod_04_1_black$cace, ivmod_04_1_hispanic$cace,
                  ivmod_04_2_white$cace,  ivmod_04_2_black$cace, ivmod_04_2_hispanic$cace,
                  ivmod_04_3_white$cace,  ivmod_04_3_black$cace, ivmod_04_3_hispanic$cace,
                  ivmod_04_4_white$cace,  ivmod_04_4_black$cace, ivmod_04_4_hispanic$cace,
                  ivmod_04_5_white$cace, ivmod_04_5_black$cace, ivmod_04_5_hispanic$cace,
                  ivmod_04_6_white$cace, ivmod_04_6_black$cace, ivmod_04_6_hispanic$cace,
                  ivmod_04__rep_white$cace,  ivmod_04__rep_black$cace, ivmod_04__rep_hispanic$cace,
                  ivmod_04_7_white$cace,  ivmod_04_7_black$cace, ivmod_04_7_hispanic$cace,
                  ivmod_04_8_white$cace,  ivmod_04_8_black$cace, ivmod_04_8_hispanic$cace
                  
                  
                  
                  
  )
  
  holder$se = c(ivmod1_white$se,  ivmod1_black$se, ivmod1_hispanic$se,
                ivmod2_white$se,  ivmod2_black$se, ivmod2_hispanic$se,
                ivmod3_white$se,  ivmod3_black$se, ivmod3_hispanic$se,
                ivmod4_white$se,  ivmod4_black$se, ivmod4_hispanic$se,
                ivmod5_white$se, ivmod5_black$se, ivmod5_hispanic$se,
                ivmod6_white$se,  ivmod6_black$se, ivmod6_hispanic$se,
                ivmod_rep_white$se,  ivmod_rep_black$se, ivmod_rep_hispanic$se,
                ivmod7_white$se,  ivmod7_black$se, ivmod7_hispanic$se,
                ivmod8_white$se, ivmod8_black$se, ivmod8_hispanic$se,
                ivmod9_white$se, ivmod9_black$se, ivmod9_hispanic$se,
                ivmod10_white$se,  ivmod10_black$se, ivmod10_hispanic$se,
                
                
                
                
                ivmod_04_1_white$se, ivmod_04_1_black$se, ivmod_04_1_hispanic$se,
                ivmod_04_2_white$se,  ivmod_04_2_black$se, ivmod_04_2_hispanic$se,
                ivmod_04_3_white$se,  ivmod_04_3_black$se, ivmod_04_3_hispanic$se,
                ivmod_04_4_white$se,  ivmod_04_4_black$se, ivmod_04_4_hispanic$se,
                ivmod_04_5_white$se,  ivmod_04_5_black$se, ivmod_04_5_hispanic$se,
                ivmod_04_6_white$se,  ivmod_04_6_black$se, ivmod_04_6_hispanic$se,
                ivmod_04__rep_white$se,  ivmod_04__rep_black$se, ivmod_04__rep_hispanic$se,
                ivmod_04_7_white$se,  ivmod_04_7_black$se, ivmod_04_7_hispanic$se,
                ivmod_04_8_white$se,  ivmod_04_8_black$se, ivmod_04_8_hispanic$se
                
                
                
                
                
                
  )
  
  holder$`p value` = c(ivmod1_white$summary$coefficients[2,4],  ivmod1_black$summary$coefficients[2,4], ivmod1_hispanic$summary$coefficients[2,4],
                       ivmod2_white$summary$coefficients[2,4],  ivmod2_black$summary$coefficients[2,4], ivmod2_hispanic$summary$coefficients[2,4],
                       ivmod3_white$summary$coefficients[2,4],  ivmod3_black$summary$coefficients[2,4], ivmod3_hispanic$summary$coefficients[2,4],
                       ivmod4_white$summary$coefficients[2,4],  ivmod4_black$summary$coefficients[2,4], ivmod4_hispanic$summary$coefficients[2,4],
                       ivmod5_white$summary$coefficients[2,4],  ivmod5_black$summary$coefficients[2,4], ivmod5_hispanic$summary$coefficients[2,4],
                       ivmod6_white$summary$coefficients[2,4],  ivmod6_black$summary$coefficients[2,4], ivmod6_hispanic$summary$coefficients[2,4],
                       ivmod_rep_white$summary$coefficients[2,4],  ivmod_rep_black$summary$coefficients[2,4], ivmod_rep_hispanic$summary$coefficients[2,4],
                       ivmod7_white$summary$coefficients[2,4],  ivmod7_black$summary$coefficients[2,4], ivmod7_hispanic$summary$coefficients[2,4],
                       ivmod8_white$summary$coefficients[2,4],  ivmod8_black$summary$coefficients[2,4], ivmod8_hispanic$summary$coefficients[2,4],
                       ivmod9_white$summary$coefficients[2,4],  ivmod9_black$summary$coefficients[2,4], ivmod9_hispanic$summary$coefficients[2,4],
                       ivmod10_white$summary$coefficients[2,4], ivmod10_black$summary$coefficients[2,4], ivmod10_hispanic$summary$coefficients[2,4],
                       
                       
                       
                       
                       ivmod_04_1_white$summary$coefficients[2,4], ivmod_04_1_black$summary$coefficients[2,4], ivmod_04_1_hispanic$summary$coefficients[2,4],
                       ivmod_04_2_white$summary$coefficients[2,4], ivmod_04_2_black$summary$coefficients[2,4], ivmod_04_2_hispanic$summary$coefficients[2,4],
                       ivmod_04_3_white$summary$coefficients[2,4],  ivmod_04_3_black$summary$coefficients[2,4], ivmod_04_3_hispanic$summary$coefficients[2,4],
                       ivmod_04_4_white$summary$coefficients[2,4],  ivmod_04_4_black$summary$coefficients[2,4], ivmod_04_4_hispanic$summary$coefficients[2,4],
                       ivmod_04_5_white$summary$coefficients[2,4],  ivmod_04_5_black$summary$coefficients[2,4], ivmod_04_5_hispanic$summary$coefficients[2,4],
                       ivmod_04_6_white$summary$coefficients[2,4],  ivmod_04_6_black$summary$coefficients[2,4], ivmod_04_6_hispanic$summary$coefficients[2,4],
                       ivmod_04__rep_white$summary$coefficients[2,4], ivmod_04__rep_black$summary$coefficients[2,4], ivmod_04__rep_hispanic$summary$coefficients[2,4],
                       ivmod_04_7_white$summary$coefficients[2,4],  ivmod_04_7_black$summary$coefficients[2,4], ivmod_04_7_hispanic$summary$coefficients[2,4],
                       ivmod_04_8_white$summary$coefficients[2,4],  ivmod_04_8_black$summary$coefficients[2,4], ivmod_04_8_hispanic$summary$coefficients[2,4]
                       
                       
                       
                       
                       
                       
  )
  
  ##############
  holder$race = c(rep(c("white", "black", "hispanic"), 20))
  
  holder$dv = c(c(rep("Vote 2016", 3),
                  rep("Vote 2014", 3),
                  rep("Vote 2012", 3),
                  rep("Vote 2010", 3),
                  rep("Democrat", 3),
                  rep("Registered", 3),
                  rep("Republican", 3)),
                rep("Vote 2008", 3),
                rep("Vote 2006", 3),
                rep("Vote 2004", 3),
                rep("Vote 2002", 3),
                
                c(rep("Vote 2016", 3),
                  rep("Vote 2014", 3),
                  rep("Vote 2012", 3),
                  rep("Vote 2010", 3),
                  rep("Democrat", 3),
                  rep("Registered", 3),
                  rep("Republican", 3)),
                rep("Vote 2008", 3),
                rep("Vote 2006", 3)
                
  )
  
  
  holder$endog = c(rep("Vote 2000", 11*3),
                   rep("Vote 2004", 9*3))
  holder$state = state
  
  
  results = rbind(results, holder)
  
  print(f); print(nrow(results))
}



rm(list=ls()[grepl('mod',ls())])
gc()

# save state-level results for later modeling
write_csv(results,'results/other_elections_state_results.csv')

data = read_csv("results/main_state_results.csv")%>%
  bind_rows(read_csv("results/other_elections_state_results.csv"))%>%
  select(coef, se, race, dv, endog, state)%>%
  filter(grepl('Vote', dv))%>%
  mutate(`2008 Election` = as.numeric(endog=='Vote 2008'),
         black = as.numeric(race=='black'),
         white = as.numeric(race=='white'),
         hispanic = as.numeric(race=='hispanic'),
         w= 1/(se^2),
         dist = as.numeric(gsub('Vote ', '',dv))- as.numeric(gsub('Vote ', '',endog)),
         presidential = as.numeric(dv %in% c('Vote 2004', 'Vote 2008', 'Vote 2012', 'Vote 2016')),
         dv.age = as.numeric(gsub('Vote ', '',dv)),
         ev.age = as.numeric(gsub('Vote ', '',endog)) )

# black mobilization effect in absolute terms'
library(fixest)
mod1=feols(coef ~ `2008 Election`+ presidential + dv.age+ev.age | state, data = data %>% filter(black==1), weights = data$w[data$black==1], cluster = ~ev.age)
mod2=feols(coef ~ `2008 Election`*black+ dv.age+ev.age | state, data = data, weights = data$w, cluster = ~ev.age)

mod1_nw=feols(coef ~ `2008 Election`+ presidential + dv.age+ev.age | state, data = data %>% filter(black==1), cluster = ~ev.age)
mod2_nw=feols(coef ~ `2008 Election`*black+ dv.age+ev.age | state, data = data, cluster = ~ev.age)



etable(mod1,mod1_nw,mod2,mod2_nw, digits =3, tex = T, fitstat = c('n','r2','wr2'), digits.stats=3,
       style.tex = style.tex("aer"), file = 'tables/Tab3.tex', replace = T)


# all races
ggsave(filename = 'figures/FigS6.pdf', width = 9, height = 5, 
plot = results %>%
  mutate(race=Hmisc::capitalize(race))%>%
  filter(endog=='Vote 2004', race!='Non-black', grepl('Vote', dv))%>%
  mutate(race = factor(race, levels = c('Hispanic','White',  'Black')))%>%
  ggplot(aes(x=coef, y = ..scaled..,weight = 1/(se^2), group = race, fill = race,linetype=race))+
  geom_density(alpha=.5)+
  xlab('State-Level Effects')+
  theme_minimal()+
  guides(fill = guide_legend(reverse = TRUE), linetype = guide_legend(reverse = TRUE))+
  scale_linetype_manual(values = c('dotted', 'dashed', 'solid'))+
  scale_fill_manual(values = c('grey','white',  'black'))+
  xlim(-0.1,1)+
  ylab('Density')+
  theme(legend.title=element_blank(), legend.position = 'bottom', legend.spacing.x = unit(.25, 'cm'),
        axis.title.y=element_blank(), axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+  facet_wrap(dv ~ ., nrow = 4)
)


ggsave(filename = 'figures/FigS8.pdf', width = 9, height = 5, 
plot = results %>%
  mutate(race=Hmisc::capitalize(race))%>%
  filter(endog=='Vote 2000', race!='Non-black', grepl('Vote', dv))%>%
  mutate(race = factor(race, levels = c('Hispanic','White',  'Black')))%>%
  ggplot(aes(x=coef, y = ..scaled..,weight = 1/(se), group = race, fill = race, linetype = race))+
  geom_density(alpha=.5)+
  xlab('State-Level Effects')+
  theme_minimal()+
  xlim(-0.75,1.75)+
  guides(fill = guide_legend(reverse = TRUE), linetype = guide_legend(reverse = TRUE))+
  scale_linetype_manual(values = c('dotted', 'dashed', 'solid'))+
  scale_fill_manual(values = c('grey','white',  'black'))+
  theme(legend.title=element_blank(), legend.position = 'bottom', legend.spacing.x = unit(.25, 'cm'),
        axis.title.y=element_blank(), axis.text.y=element_blank(),
        axis.ticks.y=element_blank())+  facet_wrap(dv ~ ., nrow = 4)
)



### Weighted Average of State CACEs

predictions_white = as.data.frame(matrix(NA, ncol = 5))
predictions_black = as.data.frame(matrix(NA, ncol = 5))
predictions_hispanic = as.data.frame(matrix(NA, ncol = 5))


colnames(predictions_white) = c("ev", "dv", "coef", "se", "race")
colnames(predictions_black) = c("ev", "dv", "coef", "se", "race")
colnames(predictions_hispanic) = c("ev", "dv", "coef", "se", "race")


predictions_white$race = "white"
predictions_black$race = "black"
predictions_hispanic$race = "hispanic"



data = results


for(i in 1:length(unique(data$endog))){
  for(j in 1:length(unique(data$dv))){
    if(length(data$dv[data$dv == unique(data$dv)[j] & data$endog==unique(data$endog)[i]]) > 0){
      
      preds_white = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "white"  & data$dv == unique(data$dv)[j] ], data$se[data$endog == unique(data$endog)[i] & data$race == "white" & data$dv == unique(data$dv)[j]])
      predictions_white = rbind(predictions_white, c(unique(data$endog)[i], unique(data$dv)[j], preds_white$est, preds_white$se, "white"))
      
      preds_black = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "black" & data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "black" & data$dv == unique(data$dv)[j]])
      predictions_black = rbind(predictions_black, c(unique(data$endog)[i], unique(data$dv)[j], preds_black$est, preds_black$se, "black"))
      
      preds_hispanic = meta.maker(data$coef[data$endog == unique(data$endog)[i] & data$race == "hispanic"  & data$dv == unique(data$dv)[j]], data$se[data$endog == unique(data$endog)[i] & data$race == "hispanic" & data$dv == unique(data$dv)[j]])
      predictions_hispanic = rbind(predictions_hispanic, c(unique(data$endog)[i], unique(data$dv)[j], preds_hispanic$est, preds_hispanic$se, "hispanic"))
      
      
      
      
      
    }
    
  }
  
}
meta_plot=rbind(predictions_white, predictions_black, predictions_hispanic)
for(i in 3:(ncol(meta_plot)-2)){
  meta_plot[,i] = as.numeric(meta_plot[,i])
}
meta_plot$se =as.numeric(meta_plot$se)
meta_plot = meta_plot[!is.na(meta_plot$ev),]
meta_plot = meta_plot[!is.nan(meta_plot$coef),]




plt = meta_plot%>%filter(ev == "Vote 2004" & race != "all" & !is.na(race))%>%
  mutate(race = factor(Hmisc::capitalize(race), levels = c('Black', 'White', 'Hispanic')))

ggsave(filename = "figures/FigS5.pdf", width=9,height=5,
plot = ggplot(data =plt[grepl('Vote', plt$dv),], aes(x = dv, y = coef, color = race, fill = race)) +
  geom_hline(yintercept = 0, linetype = 'dotted', color = 'grey60') +
  geom_point(shape = 21, size = 6, position=position_dodge(.5), alpha = .75) + xlab('') +
  ylab('Effect of 2004 Voting on Future Voting') + 
  geom_errorbar(aes(ymin=coef-1.96*se, ymax=coef+1.96*se, linetype = race), width=0,
                position=position_dodge(.5)) +
  scale_color_manual(values = c("black", "black", "black"))+
  scale_fill_manual(values = c("black", "white", "grey"))+
  scale_linetype_manual(values = c('solid', 'dotdash', 'dashed'))+
  theme_minimal()+
  scale_x_discrete(labels = c("2006", "2008", "2010", "2012", '2014', '2016'))+
  theme(axis.text.y=element_text(face='bold', size =14), axis.title.y=element_text(face='bold', size =14), 
        axis.text.x = element_text(face="bold", size =14), legend.text=element_text(face='bold', size =14),
        legend.position="bottom", legend.spacing.x = unit(.25, 'cm'),legend.title = element_blank(), legend.key.width=unit(4,"line"))+
  NULL
)




plt = meta_plot%>%filter(ev == "Vote 2000" & race != "all" & !is.na(race))%>%
  mutate(race = factor(Hmisc::capitalize(race), levels = c('Black', 'White', 'Hispanic')))

ggsave(filename = "figures/FigS7.pdf", width=9,height=5,
plot = ggplot(data = plt[grepl('Vote', plt$dv),], aes(x = dv, y = coef, color = race, fill = race)) +
  geom_hline(yintercept = 0, linetype = 'dotted', color = 'grey60') +
  geom_point(shape = 21, size = 6, position=position_dodge(.5), alpha = .75) + xlab('') +
  ylab('Effect of 2000 Voting on Future Voting') + 
  geom_errorbar(aes(ymin=coef-1.96*se, ymax=coef+1.96*se, linetype = race), width=0,
                position=position_dodge(.5)) +
  scale_color_manual(values = c("black", "black", "black"))+
  scale_fill_manual(values = c("black", "white", "grey"))+
  scale_linetype_manual(values = c('solid', 'dotdash', 'dashed'))+
  theme_minimal()+
  scale_x_discrete(labels = c('2002', '2004', "2006", "2008", "2010", "2012", '2014', '2016'))+
  theme(axis.text.y=element_text(face='bold', size =14), axis.title.y=element_text(face='bold', size =14), 
        axis.text.x = element_text(face="bold", size =14), legend.text=element_text(face='bold', size =14),
        legend.position="bottom", legend.spacing.x = unit(.25, 'cm'),legend.title = element_blank(), legend.key.width=unit(4,"line"))+
  NULL
)



